﻿// ----------------------------------------------------------------------------------
// Microsoft Developer & Platform Evangelism
// 
// Copyright (c) Microsoft Corporation. All rights reserved.
// 
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
// EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES 
// OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
// ----------------------------------------------------------------------------------
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
// ----------------------------------------------------------------------------------

namespace Microsoft.Samples.WindowsPhoneCloud.Phone.PivotContent
{
    using System;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Controls.Primitives;
    using System.Windows.Navigation;
    using Microsoft.Phone.Shell;
    using Microsoft.Samples.WindowsPhoneCloud.Phone.ViewModel;
    using Microsoft.Samples.WindowsPhoneCloud.StorageClient;

    public partial class ListQueuesPage : UserControl
    {
        public ListQueuesPage()
        {
            this.InitializeComponent();

            this.Loaded += this.OnListQueues;
        }

        public event EventHandler<NavigationEventArgs> Navigate
        {
            add { this.ViewModel.Navigate += value; }
            remove { this.ViewModel.Navigate -= value; }
        }

        public ListQueuesPageViewModel ViewModel
        {
            get { return this.DataContext as ListQueuesPageViewModel; }
            set { this.DataContext = value; }
        }

        private static void SetApplicationState(string key, object value)
        {
            if (PhoneApplicationService.Current.State.ContainsKey(key))
            {
                PhoneApplicationService.Current.State.Remove(key);
            }

            PhoneApplicationService.Current.State.Add(key, value);
        }
        
        private void OnListQueues(object sender, RoutedEventArgs e)
        {
            if (this.ViewModel != null)
            {
                this.ViewModel.ListQueues();
            }
        }

        private void OnDeleteQueue(object sender, EventArgs e)
        {
            var queue = ((Button)sender).Tag as ICloudQueue;
            if ((this.ViewModel != null) && (queue != null))
            {
                this.ViewModel.DeleteQueue(queue);
            }
        }

        private void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.ViewModel != null)
            {
                var selector = sender as Selector;
                if ((selector == null) || (selector.SelectedIndex == -1))
                {
                    return;
                }

                SetApplicationState("CurrentQueue", this.ViewModel.CloudQueues[selector.SelectedIndex]);
                this.ViewModel.NavigateTo(new Uri("/Pages/QueueDetailsPage.xaml", UriKind.Relative));
                selector.SelectedIndex = -1;
            }
        }
    }
}